package com.stupidzhang.weixin.task;

import com.stupidzhang.weixin.config.OrderProperties;
import com.stupidzhang.weixin.constant.JdConstants;
import com.stupidzhang.weixin.mode.WeiXinMessageEntity;
import com.stupidzhang.weixin.service.JingFenApiService;
import com.stupidzhang.weixin.service.JingFenAppService;
import com.stupidzhang.weixin.service.WeiXinApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author lyqiang
 */
@Slf4j
@Component
public class ZhiHuForJdScheduled {
    @Autowired
    private OrderProperties orderProperties;
    @Autowired
    private WeiXinApiService weiXinApiService;
    @Autowired
    private JingFenAppService jingFenApiService;
    @Autowired
    private JingFenApiService jingFenApiNewService;


    @Scheduled(cron = "0 0/5 * * * ?")
    public void scheduledQueryOrderList() {
        LocalDateTime nowTime = LocalDateTime.now().withNano(0);
        LocalDateTime fiveMinBefore = nowTime.minusMinutes(5);
        String endTimeStr = nowTime.format(DateTimeFormatter.ofPattern(JdConstants.DATE_TIME_FORMAT));
        String startTimeStr = fiveMinBefore.format(DateTimeFormatter.ofPattern(JdConstants.DATE_TIME_FORMAT));
        WeiXinMessageEntity entity = jingFenApiNewService.queryOrderList(startTimeStr, endTimeStr);
        if (entity != null) {
            // 发送消息
            weiXinApiService.sendMessage(entity, orderProperties.getTemplateId());
        }
    }

    @Scheduled(cron = "0 59 11,23 * * ?")
    public void scheduledSummary() {
        LocalDateTime nowTime = LocalDateTime.now().withNano(0);
        jingFenApiService.scheduledFromJdSummary(nowTime, nowTime);
    }

}
